import numpy as np
import matplotlib.pyplot as plt
import matplotlib


## enable latex
from matplotlib import rc
rc('font',**{'family':'serif','serif':['Times']})
rc('text', usetex=True)
width = 0.36
##


def read_file(filename):
    ns = []
    tqDistTimes = []
    qDistTimes  = []
    with open(filename, 'r') as fin:
        for line in fin:
            n_str, d_str, tqDistTime_str, qDistTime_str = line.strip().split()
            ns.append(int(n_str))
            tqDistTimes.append(float(tqDistTime_str))
            qDistTimes.append(float(qDistTime_str))

    return ns, tqDistTimes, qDistTimes

if __name__ == "__main__":
    filename = "gen.out"
    ns, tqDistTimes, qDistTimes = read_file(filename)

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.set_yscale("log")
    ax.set_xscale("log")

    plt.title(r"Quartet distance running times on trees with degree 128")
    plt.plot(ns, tqDistTimes, 'k',   linewidth=1.5, label=r"tqDist - $O(d\cdot n \cdot \log n)$")
    plt.plot(ns, qDistTimes,  'k--', linewidth=1.5, label=r"qDist 2011 - $O(n^{2.688})$")

    plt.ylim(3e-3, max(qDistTimes)*1.07)
    
    plt.ylabel(r"\textit{Running time in seconds}")
    plt.xlabel(r"\textit{Number of leaves}")

    plt.legend(shadow=True, fancybox=True, loc="best")

    print "saving plot"
    filename = "general_trees.eps"
    plt.savefig(filename)
    print "-- plot saved to", filename
